package com.example.mvvm.ui.my

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.mvvm.R
import com.example.mvvm.data.PlaceRepository
import com.example.mvvm.data.db.CoolWeatherDatabase
import com.example.mvvm.data.model.place.Province
import com.example.mvvm.data.network.CoolWeatherNetwork
import com.example.mvvm.databinding.FragmentMycomicBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class MyComicFragment : Fragment() {
    var listData: MutableList<Province> = mutableListOf()
    companion object{
        @JvmStatic
        var myComicFragment:MyComicFragment?=null
    }
    val viewmodel by lazy {
        val instance = PlaceRepository.getInstance(
            CoolWeatherDatabase().getPlaceDao(),
            CoolWeatherNetwork.getInstance()
        )
        ViewModelProvider(this, MyViewModel.ViewModelFacoty(instance)).get(MyViewModel::class.java)
    }
    lateinit var binding: FragmentMycomicBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_mycomic, container, false)

        //binding 和lifecycler要是同一个owner，要不xml数据绑定不是双向的
        binding.lifecycleOwner = this
        binding.viewModel = viewmodel

        myComicFragment = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        lifecycleScope.launchWhenCreated {
            viewmodel.getProvinces()

        }

//        val myAdapter = MyComicAdapter(context, listData)
//        binding.lvPro.adapter = myAdapter
//        binding.lvPro.layoutManager = LinearLayoutManager(context)

//        viewmodel.provinces.observe(viewLifecycleOwner, Observer {
//            viewmodel.name.value = it.size.toString()
//            Log.e("Wodegg",it.size.toString())
////            myAdapter.list = it
////            myAdapter.notifyDataSetChanged()
//            Log.e("Wodegg","it.size.toString()")
//        })

        observe()
    }

    private fun observe() {
        viewmodel.selectedPositon.observe(viewLifecycleOwner, Observer {
            val i = Intent(context, WetherActivity::class.java)
            i.putExtra("position", it)
            startActivity(i)
        })
    }

    suspend fun getProvinceList() = withContext(Dispatchers.IO) {
        val coolWeatherNetwork = CoolWeatherNetwork()
        val fetchProvinceList = coolWeatherNetwork.fetchProvinceList()
        /*
        foreach快捷键是iter
         */
        for (province in fetchProvinceList) {
            Log.e("wode", province.provinceName)
        }
    }
}
